<%@ page import="java.util.*"%>
<%@ page import="objects.*"%>
<%@ page import="Domain.UserClient.ButtonsFunctions"%>
<%@page import="Domain.DataManager"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%
	DataManager dm = (DataManager) (session.getAttribute("dm"));
	ButtonsFunctions bf = (ButtonsFunctions) (session
			.getAttribute("bf"));

	if (dm == null || dm.getUserStatus() == DataManager.DISCONNECTED) {
		response.sendRedirect("Welcome.jsp");
	} else {
%>

<%
	dm.setCurrThread(null);
	dm.setCurrForum(null);
%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="style.css" type="text/css">

<title>Manage Friends</title>

<link rel="stylesheet" href="windowfiles/dhtmlwindow.css"
	type="text/css" />

<script type="text/javascript" src="windowfiles/dhtmlwindow.js"></script>

<script language="javascript" type="text/javascript">
	//Browser Support Code
	function updateUsingAjax() {
		var ajaxRequest;

		// The variable that makes Ajax possible! 
		try {
			// Opera 8.0+, Firefox, Safari 
			ajaxRequest = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer Browsers 
			try {
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					// Something went wrong 
					alert("Your browser broke!");
					return false;
				}
			}
		}

		// Create a function that will receive data sent from the server 
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				var response = ajaxRequest.responseText;			
				if(response.trim() == "")
				{
					alert("You have been disconnected from the server!");
					parent.location = 'Welcome.jsp';
				}
				else
					document.getElementById("numOfNotifications").innerHTML = response;
			}
		};

		var url = "GetNumOfNotifications.jsp";
		var params = "";
		ajaxRequest.open("POST", url, true);

		//Send the proper header information along with the request
		ajaxRequest.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close");

		ajaxRequest.send(params);
	}

	function updateNumOfNotifications() {
		updateUsingAjax();
		setTimeout("updateNumOfNotifications()", 3000);
	}

	function openmypage() { //Define arbitrary function to run desired DHTML Window widget codes
		ajaxwin = dhtmlwindow
				.open("ajaxbox", "ajax", "GetNotification.jsp",
						"Notification window",
						"width=450px,height=300px,left=300px,top=100px,resize=1,scrolling=1");
		ajaxwin.onclose = function() {
			return window.confirm("Close notification window?");
		}
	}
	
	function promoteUser(){
		var userLst = document.myForm.allUsers;
        var selectedUser = userLst.value;
        
        if(selectedUser != "" & selectedUser != null)
        {
    		var ajaxRequest;

    		// The variable that makes Ajax possible! 
    		try {
    			// Opera 8.0+, Firefox, Safari 
    			ajaxRequest = new XMLHttpRequest();
    		} catch (e) {
    			// Internet Explorer Browsers 
    			try {
    				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try {
    					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {
    					// Something went wrong 
    					alert("Your browser broke!");
    					return false;
    				}
    			}
    		}

    		// Create a function that will receive data sent from the server 
    		ajaxRequest.onreadystatechange = function() {
    			if (ajaxRequest.readyState == 4) {
    				userLst.selectedIndex = -1;
    				document.myForm.userDetails.innerHTML = "";
    			}
    		};
    		
    		var url = "PromoteUser.jsp";
    		var params = "nameOfUser=" + selectedUser;
    		ajaxRequest.open("POST", url, true);

    		//Send the proper header information along with the request
    		ajaxRequest.setRequestHeader("Content-type",
    				"application/x-www-form-urlencoded");
    		ajaxRequest.setRequestHeader("Content-length", params.length);
    		ajaxRequest.setRequestHeader("Connection", "close");

    		ajaxRequest.send(params);
        }
	}
	
	function removeUser(){
		var userLst = document.myForm.allUsers;
        var selectedUser = userLst.value;	
        
        if(selectedUser != "" & selectedUser != null)
        {
    		var ajaxRequest;

    		// The variable that makes Ajax possible! 
    		try {
    			// Opera 8.0+, Firefox, Safari 
    			ajaxRequest = new XMLHttpRequest();
    		} catch (e) {
    			// Internet Explorer Browsers 
    			try {
    				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try {
    					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {
    					// Something went wrong 
    					alert("Your browser broke!");
    					return false;
    				}
    			}
    		}

    		// Create a function that will receive data sent from the server 
    		ajaxRequest.onreadystatechange = function() {
    			if (ajaxRequest.readyState == 4) {
    				var userLstLength = userLst.length; 
    				for(i = userLstLength - 1; i >= 0; i--)
					{
						if(userLst[i].selected)
						{
							deleteOption(userLst, i);
						}
					}
    			}
    		};

    		var url = "DeleteUser.jsp";
    		var params = "nameOfUser=" + selectedUser;
    		ajaxRequest.open("POST", url, true);

    		//Send the proper header information along with the request
    		ajaxRequest.setRequestHeader("Content-type",
    				"application/x-www-form-urlencoded");
    		ajaxRequest.setRequestHeader("Content-length", params.length);
    		ajaxRequest.setRequestHeader("Connection", "close");

    		ajaxRequest.send(params);
        }
	}
	
	function showUserType(){
		var userLst = document.myForm.allUsers;
        var selectedUser = userLst.value;	
        
        if(selectedUser != "" & selectedUser != null)
        {
    		var ajaxRequest;

    		// The variable that makes Ajax possible! 
    		try {
    			// Opera 8.0+, Firefox, Safari 
    			ajaxRequest = new XMLHttpRequest();
    		} catch (e) {
    			// Internet Explorer Browsers 
    			try {
    				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    			} catch (e) {
    				try {
    					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    				} catch (e) {
    					// Something went wrong 
    					alert("Your browser broke!");
    					return false;
    				}
    			}
    		}

    		// Create a function that will receive data sent from the server 
    		ajaxRequest.onreadystatechange = function() {
    			if (ajaxRequest.readyState == 4) {
	    			var response = ajaxRequest.responseText;
	    			document.myForm.userDetails.innerHTML = response.trim();
				}
    		};

    		var url = "GetUserDetails.jsp";
    		var params = "nameOfUser=" + selectedUser;
    		ajaxRequest.open("POST", url, true);

    		//Send the proper header information along with the request
    		ajaxRequest.setRequestHeader("Content-type",
    				"application/x-www-form-urlencoded");
    		ajaxRequest.setRequestHeader("Content-length", params.length);
    		ajaxRequest.setRequestHeader("Connection", "close");

    		ajaxRequest.send(params);
        }
	}

	function deleteOption(theSel, theIndex)
	{ 
	  var selLength = theSel.length;
	  if(selLength>0)
	  {
	    theSel.options[theIndex] = null;
	  }
	}
	
</script>

</head>
<body>

	<table width="100%" height="100%" border="1" cellspacing="0"
		cellpadding="0">

		<!-- HEADER -->
		<tr class="header">
			<td>
				<!-- cool navigation toolbar code -->
				<ul id="navlist">
					<li id="logout"><a href="Logout.jsp"></a></li>
					<li id="prev"><a href="ChooseForum.jsp"></a></li>
					<li id="notifications"><a href="#"
						onClick="openmypage(); return false"></a></li>
					<li id="notificationCounter"><center>
							<font size=3 id="numOfNotifications"></font>
						</center>
					</li>
					<li id="connection">Connected as: <b><i><font
								color='#FF0000'> <%
 	out.print(dm.getCurrUser());
 %> </font> </i> </b></li>
				</ul>
			</td>
		</tr>

		<!-- BODY -->
		<tr class="body">
			<td class="forumBody">

				<center>
					<h6>
						<font color="#FFF11" size="7"><i>Manage your friends</i>
						</font>
					</h6>
				</center>

				<br> <br> <br>
				<div align="center">
					<form name="myForm">
						<table align="center" cellspacing="2" cellpadding="2" border="0">

							<tr>
								<td colspan=2>
								<center>
									<input type="button" name="promote" onClick="promoteUser();" value="Promote user"> 
									<input type="button" name="remove" onClick="removeUser();" value="Remove user">
								</center>
								</td>
							</tr>
							<tr>
								<td width='200'>
								<center>
								<font color='yellow'>
								<b>All Users</b>
								<br><br>
								<select name="allUsers" size="20" onchange="showUserType();">

										<%
											bf.processGetUsers();
											Vector<User> users = dm.getVectorUser();

											for (int i = 0; i < users.size(); i++) {
												String user = users.elementAt(i).getUserName();
												String currUser = dm.getCurrUser();
												if(!user.equals(currUser))
												{
													out.print("<option>" + user + "</option>");	
												}
											}
										%>

								</select>
								</font>
								</center>
								</td>
								<td>
									<center>
									<font color='yellow'>
									<b>User Details:</b>
									<br><br>
									<textarea cols="40" rows="5" id='userDetails' readonly></textarea>	
									</font>
									</center>					
								</td>
							</tr>
						</table>
					</form>
				</div>
			</td>
		</tr>
	</table>
	<script language="javascript" type="text/javascript">updateNumOfNotifications();</script>

</body>
</html>
<%
	}
%>